#include"Task.hpp"
#include"blockqueue.hpp"
#include<unistd.h>
#include<time.h>
#include<stdlib.h>


void *Consumer(void*args)
{
    BlockQueue<Task>* bq=static_cast<BlockQueue<Task>*>(args);
    while(true)
    {
        //获取数据
        Task task;
        bq->Pop(&task);
        task();
        std::cout<<task.result()<<std::endl;
        
    }
    std::cout<<" Consumer excute done "<<std::endl;
    return nullptr;
}


void *Productor(void*args)
{
    srand((unsigned int)time(nullptr)^getpid());
    BlockQueue<Task>*bq=static_cast<BlockQueue<Task>*>(args);
    while(true)
    {
        //制造数据

        int x=rand()%1000;
        usleep(1000);
        int y=rand()%1000;
        Task t(x,y);
        bq->Equeue(t);
        std::cout<<"productor make a task"<<std::endl;
    }
    std::cout<<" Consumer excute done "<<std::endl;
    return nullptr;
}

int main()
{
    BlockQueue<Task_t>*bq=new BlockQueue<Task_t>();
    pthread_t c1,c2,c3,p2,p3,p1;
    pthread_create(&c1,nullptr,Consumer,bq);
    pthread_create(&c2,nullptr,Consumer,bq);
    pthread_create(&c3,nullptr,Consumer,bq);

    pthread_create(&p1,nullptr,Productor,bq);
    pthread_create(&p2,nullptr,Productor,bq);
    pthread_create(&p3,nullptr,Productor,bq);


    pthread_join(c1,nullptr);
    pthread_join(p1,nullptr);
    pthread_join(p2,nullptr);
    pthread_join(p3,nullptr);
    pthread_join(c2,nullptr);
    pthread_join(c3,nullptr);
    

    return 0;
}

